<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f4;
        }

        .container {
            width: 80%;
            margin: 20px auto;
            overflow: hidden;
            background: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        header {
            background: #333;
            color: #fff;
            padding: 20px;
            text-align: center;
        }

        nav {
            display: flex;
            justify-content: center;
            background: #555;
            padding: 10px 0;
        }

        nav a {
            color: white;
            text-decoration: none;
            padding: 0 15px;
        }

        nav a:hover {
            background: #444;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        table,
        th,
        td {
            border: 1px solid #ddd;
        }

        th,
        td {
            padding: 12px;
            text-align: left;
        }

        th {
            background-color: #333;
            color: #fff;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        .actions {
            text-align: center;
        }

        .edit,
        .delete,
        .reset-password {
            padding: 5px 10px;
            background: #333;
            color: #fff;
            border: none;
            cursor: pointer;
            text-decoration: none;
            margin: 2px;
            display: inline-block;
        }

        .edit:hover,
        .delete:hover,
        .reset-password:hover {
            background: #444;
        }

        footer {
            background: #333;
            color: #fff;
            text-align: center;
            padding: 10px 0;
            position: relative;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>

<body>
    <header>
        <h1>用户管理</h1>
    </header>
    <nav>
        <a href="index.html">返回首页</a>
        <a href="add_user.html">添加用户</a>
    </nav>
    <div class="container">
        <h2>用户列表</h2>
        <table>
            <thead>
                <tr>
                    <th>用户名</th>
                    <th>邮箱</th>
                    <th>角色</th>
                    <th>注册日期</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody class="tbody">
                <tr>
                    <td>admin</td>
                    <td>admin@qq.com</td>
                    <td>admin</td>
                    <td>2024-01-01</td>
                    <td>true</td>
                    <td class="actions">
                        <a href="edit_user.html?id=${item.id}" class="edit">编辑</a>
                        <a href="javascript:void(0);" class="delete">删除</a>
                        <a href="reset_password.php?id=${item.id}" class="reset-password">重置密码</a>
                    </td>
                </tr>
            </tbody>
            <!-- 复制上面的 <tr> 来创建更多的用户条目 -->
        </table>
    </div>
    <footer>
        <p>版权所有 © 2024 新闻网站</p>
    </footer>
</body>
<script src="./js/jquery-3.7.1.min.js"></script>
<script>
    getUserList()
    function getUserList() {
        $.ajax({
            url: "http://www.dlrb.com:8082/getUserList.php",
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                    let context = ""
                    for (let item of resp.data) {
                        context += `
                        <tr>
					        <td>${item.username}</td>
					        <td>${item.mail}</td>
					        <td>${item.role}</td>
					        <td>${item.register}</td>
                            <td>${item.status == 'true'? '激活' :"未激活"}</td>
					        <td class="actions">
                                <a href="edit_user.html?uid=${item.id}" class="edit">编辑</a>
                                <a href="javascript:void(0);" onclick=deleteById('${item.id}','${item.username}') class="delete">删除</a>
                                <a href="reset_password.html?uid=${item.id}" class="reset-password">重置密码</a>
                            </td>
				        </tr>
                        `
                    }
                    $(".tbody").html(context)
                } else {
                    alert(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    }
    function deleteById(uid,deleteUser){
        let cookie = cookieToJson(document.cookie)
        let username = cookie.username
        let role = cookie.role

        $.ajax({
            url: "http://www.dlrb.com:8082/deleteUserById.php",
            type: "GET",
            xhrFields: {
                withCredentials: true
            },
            data: {
                username,
                deleteUser,
                role,
                uid
            },
            dataType: "json",
            success: (resp) => {
                if (resp.code == 200) {
                    alert(resp.msg)
                    getUserList()
                } else {
                    alert(resp.msg)
                }
            },
            error: (resp) => {
                alert(resp.msg)
            }
        })
    }

    function cookieToJson(cookieString) {
        // 处理空字符串
        if (!cookieString || typeof cookieString !== 'string') {
            return {};
        }
        // 分割 Cookie 字符串
        const cookies = cookieString.split(';');
        const result = {};
        // 遍历每个 Cookie
        for (const cookie of cookies) {
            // 跳过空字符串
            if (!cookie.trim()) {
                continue;
            }
            // 查找第一个等号位置
            const equalsIndex = cookie.indexOf('=');

            // 如果没有等号，跳过这个 Cookie
            if (equalsIndex === -1) {
                continue;
            }
            // 提取键和值
            const name = cookie.substring(0, equalsIndex).trim();
            let value = cookie.substring(equalsIndex + 1).trim();
            // 处理可能的 URL 编码
            try {
                value = decodeURIComponent(value);
            } catch (e) {
                // 如果解码失败，使用原始值
                console.warn(`Failed to decode cookie value: ${value}`, e);
            }
            // 添加到结果对象
            result[name] = value;
        }
        return result;
    }
</script>

</html>